﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace AvantGameMachine.Graphics
{
    public struct PosVector
    {
        private float x;
        private float y;
        private float z;

        public PosVector(float _v)
        {
            x = _v;
            y = _v;
            z = _v;
        }

        public PosVector(Point _p)
        {
            x = _p.X;
            y = _p.Y;
            z = 0;
        }

        public PosVector(PointF _p)
        {
            x = _p.X;
            y = _p.Y;
            z = 0;
        }

        public PosVector(float _x, float _y)
        {
            x = _x;
            y = _y;
            z = 0;
        }

        public PosVector(float _x, float _y, float _z)
        {
            x = _x;
            y = _y;
            z = _z;
        }

        public PosVector(Size2D _size)
        {
            x = _size.Widthf;
            y = _size.Heightf;
            z = 0;
        }

        public float X
        {
            get { return x; }
            set { x = value; }
        }

        public float Y
        {
            get { return y; }
            set { y = value; }
        }

        public float Z
        {
            get { return z; }
            set { z = value; }
        }

        public int XInt
        {
            get { return (int)x; }
            set { x = value; }
        }

        public int YInt
        {
            get { return (int)y; }
            set { y = value; }
        }

        public int ZInt
        {
            get { return (int)z; }
            set { z = value; }
        }

        public PosVector GetUnit()
        {
            float x1 = x;
            float y1 = y;
            float z1 = z;

            float max = (x1 > y1) ? x1 : y1;
            max = (max > z1) ? max : z1;

            if (max == 0.0f)
            {
                max = (x1 < y1) ? x1 : y1;
                max = (max < z1) ? max : z1;
            }

            if (max != 0.0f && (max > 1.0f || max < -1.0f))
            {
                if (max < -1.0f)
                    max *= -1.0f;

                x1 /= max;
                y1 /= max;
                z1 /= max;
            }

            return new PosVector(x1, y1, z1);
        }

        public PosVector MakeUnit()
        {
            PosVector temp = GetUnit();
            x = temp.x;
            y = temp.y;
            z = temp.z;

            return this;
        }

        public float Module()
        {
            return (float)(Math.Sqrt(x * x + y * y + z * z));
        }

        public static PosVector operator +(PosVector _left, PosVector _right)
        {
            return new PosVector(_left.x + _right.x, _left.y + _right.y, _left.z + _right.z);
        }

        public static PosVector operator -(PosVector _left, PosVector _right)
        {
            return new PosVector(_left.x - _right.x, _left.y - _right.y, _left.z - _right.z);
        }

        public static PosVector operator *(PosVector _left, float _right)
        {
            return new PosVector(_left.x * _right, _left.y * _right, _left.z * _right);
        }

        public override string ToString()
        {
            return "[" + x + "|" + y + "|" + z + "]";
        }

        public float Length()
        {
            return (float)(Math.Sqrt(x*x + y*y + z*z));
        }

        public PosVector Normalize()
        {
            PosVector res = new PosVector();
            float l = Length();
            if (l == 0.0f)
            {
                return new PosVector(0.0f, 0.0f, 0.0f);
            }
            res.x = x / l;
            res.y = y / l;
            res.z = z / l;
            return res;
        }

        public PosVector CrossProduct(PosVector v)
        {
            PosVector resVector;
            resVector.x = y * v.z - z * v.y;
            resVector.y = z * v.x - x * v.z;
            resVector.z = x * v.y - y * v.x;

            return resVector;
        }

    }
}
